﻿using System;
using System.Linq;
using System.Web.UI.WebControls;
using FatFreeFitness._models;
using FatFreeFitness.membership;

namespace FatFreeFitness._secured.members
{
    public partial class WorkOut : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack && !Page.IsCallback)
            {
                // EXISTS(SELECT VALUE m FROM it.Members AS m WHERE m.ID = @MemberID)

                using (var entities = new FatFreeFitnessEntities())
                {
                    int memberID = MembershipHelper.GetMemberID();

                    var member = entities.Members.Where(m => m.ID == memberID).First();

                    if (member.RoutineID.HasValue)
                    {
                        hidRoutineID.Value = member.RoutineID.Value.ToString();
                    }
                }

            }
        }

        protected void btnRecordDetails_Click(object sender, EventArgs e)
        {
            using (var entities = new FatFreeFitnessEntities())
            {
                var workout = new _models.WorkOut()
                                  {
                                      MemberID = MembershipHelper.GetMemberID(),
                                      Date = (DateTime)RadDatePicker1.DbSelectedDate,
                                  };

                entities.WorkOuts.AddObject(workout);
                entities.SaveChanges();

                hidWorkoutID.Value = workout.ID.ToString();

            }

            RadMultiPage1.SelectedIndex = 1;
        }

        protected void FormView2_ItemInserted(object sender, FormViewInsertedEventArgs e)
        {
            //e.KeepInInsertMode = true;
            var formview = sender as FormView;
            //formview.ChangeMode(FormViewMode.ReadOnly);
            formview.Visible = false;
        }
    }
}